package com.tian.strategy;

import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * {@code @description:} 折扣
 *
 * @author tianwc 公众号：Java后端技术全栈
 * 在线刷题 1200+java面试题和1000+篇技术文章：<a href="https://woaijava.cc/">博客地址</a>
 * {@code @date:} 2024-11-19 22:18
 * {@code @version:} 1.0
 */
@Service
public class CalculateDiscountAmount4DiscountServiceImpl implements CalculateDiscountAmountService {

    @Override
    public BigDecimal calculateDiscountAmount(BigDecimal totalAmount, BigDecimal couponFaceAmount) {
        //比例值 couponFaceAmount=1 表示一折，乘以10后的比例值为10% 0.1
        BigDecimal proportion = couponFaceAmount.divide(new BigDecimal("10"), 2, RoundingMode.HALF_UP);
        //优惠金额=总金额*比例值/100
        BigDecimal discountAmount = totalAmount.multiply(proportion).setScale(3, RoundingMode.HALF_UP);
        //实际支付金额=总金额-优惠金额
        return discountAmount.setScale(2, RoundingMode.HALF_UP);
    }
}
